#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

int gCount = 0;
mutex mtx;

class MutexLockGard
{
public:
    MutexLockGard(mutex &mtx)
        : _mtx(mtx)
    {
        _mtx.lock();
    }

    ~MutexLockGard()
    {
        _mtx.unlock();
    }

private:
    mutex &_mtx;
};

void threadFunc(void)
{
    for (size_t i = 0; i < 10000000; i++)
    {
        MutexLockGard autoLock(mtx);
        gCount++;
    }
}

int main()
{
    thread th1(threadFunc);
    thread th2(threadFunc);
    th1.join();
    th2.join();
    cout << "gCount = " << gCount << endl;
    return 0;
}